#!/usr/bin/python

import os, traceback, sys
from optparse import OptionParser

INPUT_FILE = 'input'
OUTPUT_FILE = 'output'
HISTORY_FILE = 'history'
ERROR_FILE = 'error'

def main():
	"""main function"""
	parser = OptionParser()
	parser.usage = '%prog [options]... <command> <function-name> <new-function-name> <filename>...'
	parser.add_option("", "--continue", action="store_true", dest="cont", default=False, help="load history")

	options, args = parser.parse_args()

	while True:
		if options.cont:
			input = 'load_history()'
			options.cont = False
		else:
			try:
				input = raw_input(">>> ")
			except:
				traceback.print_exc()

		if input == 'exit':
			exit()

		fp = open(INPUT_FILE, 'w')
		fp.write(input)
		fp.close()

		fp = open(HISTORY_FILE, 'a')
		fp.write(input + os.linesep)
		fp.close()

		while os.path.exists(INPUT_FILE):
			pass

		while not os.path.exists(OUTPUT_FILE):
			pass
	 
		sys.stdout.write(open(OUTPUT_FILE).read())
	 
		if os.path.exists(ERROR_FILE):
			sys.stderr.write(open(ERROR_FILE).read())

		os.remove(ERROR_FILE)
		os.remove(OUTPUT_FILE)

if __name__ == '__main__':
	main()
